package com.qingrongby.shop.util;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.util.ArrayList;
import java.util.List;

/**
 * @CLASSNAME ConverterListAndJson
 * @DESCRIPTION TODO
 * @AUTHOR hzp
 * @DATE 2020/6/29
 * @VERSION 1.0
 **/
@Converter
public class ConverterListAndJson implements AttributeConverter<List<Object>,String> {

    @Override
    public String convertToDatabaseColumn(List<Object> objects) {
        if(objects.isEmpty()){
            return "";
        }
        return JSON.toJSONString(objects);
    }

    @Override
    public List<Object> convertToEntityAttribute(String s) {
        if(StringUtils.isBlank(s)){
            return null;
        }

        return JSON.parseObject(s, ArrayList.class);
    }
}
